package masterjava.common.client.gxt.base.tabs;

import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.google.gwt.user.client.Element;

import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;

/**
 * User: gkislin
 * Date: 7/7/11
 */
abstract public class BaseTabs<T> extends TabPanel {
    private final Iterator<T> itemIterator;

    public BaseTabs(T tabItem) {
        this(Collections.singletonList(tabItem));
    }

    public BaseTabs(T[] tabItems) {
        this(Arrays.asList(tabItems));
    }

    public BaseTabs(Iterable<T> tabIterable) {
        this.itemIterator = tabIterable.iterator();
    }

    @Override
    protected void onRender(Element parent, int pos) {
        super.onRender(parent, pos);
        while (itemIterator.hasNext()) {
            TabItem tabItem = createTabItem(itemIterator.next());
            add(tabItem);
        }
//        setResizeTabs(true);
        setAutoHeight(true);
    }

    abstract protected TabItem createTabItem(T next);

    @Override
    protected void onResize(int width, int height) {
        super.onResize(width, height);
        doLayout();
    }
}
